package cn.tedu.autoshow.website.service.impl;

import cn.tedu.autoshow.commons.ex.ServiceException;
import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.util.PageInfoToPageDataConverter;
import cn.tedu.autoshow.commons.web.ServiceCode;
import cn.tedu.autoshow.website.mapper.PaperDetailMapper;
import cn.tedu.autoshow.website.mapper.PaperMapper;
import cn.tedu.autoshow.website.pojo.entity.Paper;
import cn.tedu.autoshow.website.pojo.entity.PaperDetail;
import cn.tedu.autoshow.website.pojo.param.PaperDetailAddNewParam;
import cn.tedu.autoshow.website.pojo.param.PaperDetailUpdateInfoParam;
import cn.tedu.autoshow.website.pojo.vo.PaperDetailListItemVO;
import cn.tedu.autoshow.website.pojo.vo.PaperDetailStandardVO;
import cn.tedu.autoshow.website.service.PaperDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class PaperDetailServiceImpl implements PaperDetailService {

    @Autowired
    PaperDetailMapper paperDetailMapper;
    @Autowired
    PaperMapper paperMapper;

    //添加稿件
    @Override
    public void addNew(PaperDetailAddNewParam paperDetailAddNewParam) {
        log.info("【开始】:【新增—稿件详情】");
        log.info("传入参数, paperDetailAddNewParam={}", paperDetailAddNewParam);

        //判断稿件是否存在,如果不存在抛出异常
        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", paperDetailAddNewParam.getId());
        int countById = paperMapper.selectCount(queryWrapper);
        log.debug("根据稿件ID统计匹配的稿件数量，结果：{}", countById);
        if (countById == 0) {
            String message = "添加稿件详情失败，稿件不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //新增稿件详情并将新的稿件详情数据添加到数据库
        PaperDetail paperDetail = new PaperDetail();
        BeanUtils.copyProperties(paperDetailAddNewParam, paperDetailAddNewParam);
        paperDetail.setGmtCreate(LocalDateTime.now())
                .setGmtModified(LocalDateTime.now());
        int rows = paperDetailMapper.insert(paperDetail);
        if (rows != 1) {
            String message = "添加稿件失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UNKNOWN, message);
        }
        log.info("将新的稿件详情数据插入到数据库，完成！");
    }

    //根据ID删除稿件详情
    @Override
    public void deleteById(Long id) {
        //判断稿件详情是否存在
        log.info("开始处理【根据ID删除稿件详情】的业务，参数：{}", id);
        // 检查稿件是否存在，如果不存在，则抛出异常
        QueryWrapper<PaperDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = paperDetailMapper.selectCount(queryWrapper);
        log.info("根据稿件详情ID统计匹配的稿件详情数量，结果：{}", countById);
        if (countById == 0) {
            String message = "删除稿件详情失败，稿件详情不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //执行删除
        int rows = paperDetailMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除品牌失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UNKNOWN, message);
        }
        log.info("根据ID删除稿件详情");
    }

    //修改稿件详情
    @Override
    public void updateInfoById(Long id, PaperDetailUpdateInfoParam paperDetailUpdateInfoParam) {
        //检查稿件详情是否存在
        log.info("开始处理【根据ID修改稿件详情】的业务，参数：{}", id);
        // 检查稿件详情是否存在，如果不存在，则抛出异常
        QueryWrapper<PaperDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = paperDetailMapper.selectCount(queryWrapper);
        log.info("根据稿件ID统计匹配的稿件数量，结果：{}", countById);
        if (countById == 0) {
            String message = "修改稿件失败，稿件数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        //将新的稿件详情到数据库
        PaperDetail paperDetail = new PaperDetail();
        BeanUtils.copyProperties(paperDetailUpdateInfoParam, paperDetail);
        paperDetail.setId(id);
        int rows = paperDetailMapper.updateById(paperDetail);
        if (rows != 1) {
            String message = "修改稿件详情失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UNKNOWN, message);
        }
        log.debug("将新的品牌数据更新入到数据库，完成！");
    }

    //根据ID查询
    @Override
    public PaperDetailStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询稿件详情】的业务，参数：{}", id);
        PaperDetailStandardVO queryResult = paperDetailMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询相册详情失败，相册数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<PaperDetailListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum, pageSize);
    }

    @Override
    public PageData<PaperDetailListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询稿件列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<PaperDetailListItemVO> list = paperDetailMapper.list();
        PageInfo<PaperDetailListItemVO> pageInfo = new PageInfo<>(list);
        PageData<PaperDetailListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }


}

